package com.sevenyear.excelanalyze.listener.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sevenyear.excelanalyze.common.utils.CalculateUtils;
import com.sevenyear.excelanalyze.entity.AquiferPermeabilityRate;
import com.sevenyear.excelanalyze.entity.IrrigationReturnflowRate;
import com.sevenyear.excelanalyze.entity.PartitionBaseInfo;
import com.sevenyear.excelanalyze.service.AquiferPermeabilityRateService;
import com.sevenyear.excelanalyze.service.PartitionBaseInfoService;

/**
 * @author ：SevenYear
 * @description：TODO
 * @date ：2021/8/29 22:42
 */
public class AquiferPermeabilityRateListener extends AnalysisEventListener<AquiferPermeabilityRate> {

    private AquiferPermeabilityRateService aquiferPermeabilityRateService;
    private PartitionBaseInfoService partitionBaseInfoService;

    public AquiferPermeabilityRateListener() {
    }

    public AquiferPermeabilityRateListener(AquiferPermeabilityRateService aquiferPermeabilityRateService, PartitionBaseInfoService partitionBaseInfoService) {
        this.aquiferPermeabilityRateService = aquiferPermeabilityRateService;
        this.partitionBaseInfoService = partitionBaseInfoService;
    }

    @Override
    public void invoke(AquiferPermeabilityRate aquiferPermeabilityRate, AnalysisContext analysisContext) {
        //设置分区主键id,分区编码partitionCode,区域编码areaCode
        QueryWrapper<PartitionBaseInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("landform", aquiferPermeabilityRate.getLandform()).eq("clay_thickness", aquiferPermeabilityRate.getClayThickness());
        PartitionBaseInfo partitionBaseInfo = partitionBaseInfoService.getOne(wrapper);
        aquiferPermeabilityRate.setPartitionId(partitionBaseInfo.getId());
        aquiferPermeabilityRate.setPartitionCode(partitionBaseInfo.getPartitionCode());
        aquiferPermeabilityRate.setAreaCode(aquiferPermeabilityRate.getArea() + aquiferPermeabilityRate.getPartitionCode());
        UpdateWrapper<AquiferPermeabilityRate> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("area_code", aquiferPermeabilityRate.getAreaCode());
        aquiferPermeabilityRateService.saveOrUpdate(aquiferPermeabilityRate, updateWrapper);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
